const co = require('co');
const logger = require('../utils/logger');
const nodemailer = require('nodemailer');

let transporter = null;

const regMail = new RegExp(/^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/);

/**
 * message.to
 * message.subject
 * message.text or message.html
 */
let sendMail = co.wrap(function* (message) {
    // console.log(process.env.MAIL_SMTP_SECURE, process.env.MAIL_SMTP_HOST, process.env.MAIL_SMTP_PORT, process.env.MAIL_AUTH_USER, process.env.MAIL_AUTH_PWD);
    if (!regMail.test(message.to)) {
        return false;
    }
    try {
        if (transporter == null) {
            let transSecure = false;
            if (process.env.MAIL_SMTP_SECURE === true || process.env.MAIL_SMTP_SECURE == 'true' || process.env.MAIL_SMTP_SECURE == '1') {
                transSecure = true;
            }
            transporter = nodemailer.createTransport({
                host: process.env.MAIL_SMTP_HOST,
                port: process.env.MAIL_SMTP_PORT,
                secure: transSecure, // true for 465, false for other ports
                auth: {
                    user: process.env.MAIL_AUTH_USER, // generated ethereal user
                    pass: process.env.MAIL_AUTH_PWD // generated ethereal password
                }
            });
        }
        var mailOptions = {
            from: process.env.MAIL_AUTH_USER,
            to: message.to,
            subject: message.subject,
            text: message.text,
            html: message.html
        };
        yield transporter.sendMail(mailOptions);
        logger.info(`HAS BEEN SENT MAIL: From ${process.env.MAIL_AUTH_USER} to ${message.to}， subject: ${message.subject} `);
    } catch (error) {
        logger.error("Error in sendMail ", error);
        return false;
    }
    return true;
});


/**
 * feedbackInfo.fromMail
 * feedbackInfo.fromEosAccount
 * feedbackInfo.feedbackTime
 * feedbackInfo.content
 */
let forwardFeedbackContent = co.wrap(function* (feedbackInfo, funSuccess) {
    let eosAccountPanel = '';
    if (feedbackInfo.fromEosAccount && feedbackInfo.fromEosAccount.length > 0) {
        eosAccountPanel = `<p>EOS Account:<strong style='padding-left:10px;'>${feedbackInfo.fromEosAccount}</strong></p>`;
    }
    let htmlContent = `<p>From User:<strong style='padding-left:10px;'>${feedbackInfo.fromMail}</strong></p>${eosAccountPanel}<p>`;
    htmlContent += `<p>Feedback Time:<strong style='padding-left:10px;'>${feedbackInfo.feedbackTime}</strong></p>`;
    htmlContent += `<div style='padding-bottom:10px;'><span style='font-style: italic;'>Feedback Content:</span></div>${feedbackInfo.content}</p><div>------------END------------</div>`;
    const message = {
        to: process.env.FORWARD_TO_FEEDBACK_MAIL,
        subject: `Feedback from ${feedbackInfo.fromMail}`,
        html: htmlContent
    };
    const isOk = yield sendMail(message);
    if (isOk === true && funSuccess) {
        funSuccess();
    }
});

module.exports = {
    sendMail,
    forwardFeedbackContent
};